package com.example.hospitalsystemclient.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.hospitalsystemclient.Dataclass.UrlData;
import com.example.hospitalsystemclient.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegisterActivity extends AppCompatActivity {

    private EditText etPatientName;
    private EditText etPatientGender;
    private EditText etPatientIdentify;
    private EditText etPatientPhone;
    private EditText etPatientPassword;
    private EditText etPatientAge;
    private EditText etPatientAddr;
    private Button btnRegister;
    private UrlData urlData = new UrlData();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        etPatientName = findViewById(R.id.etPatientName);
        etPatientGender = findViewById(R.id.etPatientGender);
        etPatientIdentify = findViewById(R.id.etPatientIdentify);
        etPatientPhone = findViewById(R.id.etPatientPhone);
        etPatientPassword = findViewById(R.id.etPatientPassword);
        etPatientAge = findViewById(R.id.etPatientAge);
        etPatientAddr = findViewById(R.id.etPatientAddr);
        btnRegister = findViewById(R.id.btnRegister);

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取用户输入的信息
                String patientName = etPatientName.getText().toString().trim();
                String patientGender = etPatientGender.getText().toString().trim();
                String patientIdentify = etPatientIdentify.getText().toString().trim();
                String patientPhone = etPatientPhone.getText().toString().trim();
                String patientPassword = etPatientPassword.getText().toString().trim();
                String patientAgeText = etPatientAge.getText().toString().trim();
                String patientAddr = etPatientAddr.getText().toString().trim();

                // 将年龄字符串转换为整数
                Integer patientAge = null;
                try {
                    patientAge = Integer.parseInt(patientAgeText);
                } catch (NumberFormatException e) {
                    Toast.makeText(RegisterActivity.this, "请输入有效的年龄", Toast.LENGTH_SHORT).show();
                    return;
                }

                OkHttpClient client = new OkHttpClient();
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("patientName",patientName);
                    jsonObject.put("patientGender",patientGender);
                    jsonObject.put("patientIdentify",patientIdentify);
                    jsonObject.put("patientPhone",patientPhone);
                    jsonObject.put("patientPassword",patientPassword);
                    jsonObject.put("patientAge",patientAge);
                    jsonObject.put("patientAddr",patientAddr);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonObject.toString());
                Request request = new Request.Builder()
                        .url(urlData.register())
                        .post(requestBody)
                        .build();
                System.out.println(jsonObject);
                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                        runOnUiThread(() -> Toast.makeText(RegisterActivity.this, "网络问题", Toast.LENGTH_SHORT).show());
                    }

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        if (response.isSuccessful()) {
                            final String responseData = response.body().string();
                            try {
                                JSONObject jsonObject = new JSONObject(responseData);
                                if (jsonObject.getBoolean("stat")) {
                                    runOnUiThread(() -> Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show());
                                    Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                                    startActivity(intent);
                                    finish();
                                } else {
                                        runOnUiThread(() -> Toast.makeText(RegisterActivity.this, "失败", Toast.LENGTH_SHORT).show());
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        } else {
                                runOnUiThread(() -> Toast.makeText(RegisterActivity.this, "错误！", Toast.LENGTH_SHORT).show());
                        }
                    }
                });
            }
        });
        TextView returnText = findViewById(R.id.returnBtn);
        String text = "<u>返回</u>";
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            returnText.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY));
        } else {
            returnText.setText(Html.fromHtml(text));
        }
        returnText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
}